const filetypeExtDict: { [key: string]: string[] } = {
  jpg: ['jpg', 'jpeg'],
  png: ['png'],
  json: ['json'],
  html: ['html', 'htm'],
  txt: ['txt'],
  disp: ['disp', 'display'],
  unknown: [],
}

export type Filetype = keyof typeof filetypeExtDict

class FileExtParser {
  filetype(path: string): Filetype {
    let filetype: Filetype = 'unknown'
    if (!path) return filetype
    let fileExtension = path.substring(path.lastIndexOf('.') + 1)
    if (!fileExtension) return filetype
    fileExtension = fileExtension.toLowerCase()
    for (const tFileType in filetypeExtDict) {
      if (filetypeExtDict[tFileType].includes(fileExtension)) {
        filetype = tFileType
      }
    }
    return filetype
  }
  removeExtension(path: string): string {
    if (!path) return path
    const indexOfLastDot = path.lastIndexOf('.')
    if (indexOfLastDot === -1) return path
    return path.substring(0, indexOfLastDot)
  }
}

const fileExtParser = new FileExtParser()

export { fileExtParser }
